
$(function(){
	heigthSearch.init();
});

var heigthSearch={
		MODULE_ID : "",
		Table_Obj : null,// 表格对象
		pageIndex:1,
		pageSize:10,
		jump_page:0,
		USER_:null,
		init : function() {
			heigthSearch.USER_= getSessoin();
			heigthSearch.MODULE_ID = GetRequest().pid;
			$(document).keydown(function (event) {
		        if (event.keyCode == 13) {
		        	heigthSearch.getData();
		        }
		    })
		},
		/**
		 * 添加条件
		 * 
		 * @param _this
		 */
		addCondition:function(_this){
			var n = $(".search_conditions .condition");
//			if(n.length>=5){
//			}
			var html ="";
			html += '<div class="condition">';
			html += '<select>';
			html += '<option value="AND">与</option>';
			html += '<option value="OR">或</option>';
			html += '<option value="NOT">非</option>';
			html += '</select>';
			html += '<select  class="filed">';
			html += '<option value="pro_name">项目名称</option>';
			html += '<option value="ent_name">企业名称</option>';
			html += '<option value="create_user_name">创建人</option>';
			html += '<option value="liable_user_name">责任人</option>';
			html += '<option value="team_members_names">小组成员</option>';
			html += '<option value="pro_remarks">项目描述</option>';
			html += '</select>';
			html += '<input type="text"placeholder="请输入检索词"/>';
			html += '<select>';
			html += '<option value="1">模糊</option>';
			html += '<option value="0">精确</option>';
			html += '</select>';
			html += '<input type="button" value="+" class="btn btn-info" onclick="heigthSearch.addCondition(this)"/>';
			html += '<input type="button" value="-" class="btn btn-danger" onclick="heigthSearch.removeCondition(this)"/>';
			html += '</div>';
			$(_this).parent("div").after(html)
		},
		/**
		 * 移除条件
		 * 
		 * @param _this
		 */
		removeCondition:function(_this){
			 $(_this).parent("div").remove();
		},
		/**
		 * 清空所有条件
		 */
		removeAllConditions:function(){
			heigthSearch.pageIndex=1;
			heigthSearch.jump_page = 0;
			// heigthSearch.search_type="talent";
			// heigthSearch.agency_type="school";
			var contents = new Array();
			var n = $(".search_conditions .condition");
			$(n).each(function(i,row){
				if(i>1){
					 $(row).remove();
				}
            })
            $(".search_conditions .isBool").val("AND")
            $(".search_conditions .LikeOrEq").val("0")
            $(".search_conditions input[type=text]").val("")
		},
		/**
		 * 获取检索条件
		 */
		getParam:function(){
			var obj ={};
			var contents = new Array();
			var n = $(".search_conditions .condition");
			$(n).each(function(i,row){
				var content = new Array();
				var ch = row.children;
				if($(ch[0]).val()=="NOT"){
					content.push("AND");
					content.push("-"+$(ch[1]).val().trim());
				}else{
					content.push($(ch[0]).val().trim());
					content.push($(ch[1]).val().trim());	
				}
				content.push($(ch[2]).val().trim());	
				content.push($(ch[3]).val().trim());
				content.join(',');
				contents.push(content)
            })
            obj.higthText=contents.join('@#@')+"@#@AND,pro_stats,0,0";
    		obj.pageIndex = 1;
    		pageSize = $("#page_size").val();
    		obj.pageSize = pageSize;
    		obj.orderBy = "create_time";
    		obj.orderDesc = 1;
    		obj.table = "data_project";
    		obj.text="";
    		return obj;
		},
		/**
		 * 获取数据
		 */
		getData:function(){
			var obj = heigthSearch.getParam();
			$.ajax({
				url : "/crm/retrieval/comSearch",
				dataType : "json",
				async : true,
				data : obj,
				type : "post", 
				success : function(data) {
					heigthSearch.initTable(data.rows);
					$("#total_records").text(data.total);
					$("#this_page").text(1);
					$("#total_page").text(data.totalPages);
					var url = "/crm/retrieval/comSearch";
					setCommon(obj, Table_Obj, url);
				},
				error : function() {
				}
			})
		},
		/**
		 * 初始化表格
		 */
		initTable:function(datas) {
			var esay = $("#TableContainer").easyTable({
				data : datas, // 初始数据，动态添加可以通过setData
				hideCheckbox : false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
				clickChangeColor:false,//单击之后切换颜色，
				doubleClickChangeColor:true,//双击之后切换颜色，
				// rowClick : function(data) {// 行点击回调，参数为改行数据
				// alert(JSON.stringify(data))
				// },
				rowDoubleClick : function(data) {// 行双击回调，参数为改行数据
					heigthSearch.lookDetail(data.uuid);
				},
				columns : [ {
					title : "项目名称",
					field : "pro_name"
				},{
					title : "企业名称",
					field : "ent_name"
				},{
					title : "负责人",
					field : "liable_user_name"
				}, {
					title : "小组成员",
					field : "team_members_names"
				}, {
					title : "开始时间",
					field : "begin_time"
				}, {
					title : "下一节点时间",
					field : "next_step_time"
				}, {
					title : "创建时间",
					field : "create_time"
				}, {
					title : "创建人",
					field : "create_user_name"
				}, {
					title : "项目描述",
					display : "none",
					field : "pro_remarks"
				}, {
					title : "操作",
					render : function(data) {
						return heigthSearch.loadOperater(data);
					}
				} ]
			});
			Table_Obj = esay;
		},
		/**
		 * 加载方法
		 * 
		 * @param data
		 * @returns {String}
		 */
		loadOperater:function(data) {
			var html = "";
			html += '<a onclick="heigthSearch.lookDetail(\'' + data.uuid + '\',this)">查看</a>';
			var str = data.edit_power_user_uuids.indexOf(heigthSearch.USER_.uuid);
			if(str!= -1){
				html += '<a class="ml10" onclick="heigthSearch.edit(' + data.id + ')">编辑</a>';
			}
			return html;
		},
		/**
		 * 检索
		 */
		searchData:function() {
			var pageSize = $("#page_size").val();
			var pageIndex = 1;
			var obj = {};
			obj.pageSize = pageSize;
			obj.pageIndex = pageIndex;
			var othr = heigthSearch.getParam();
			reset_serach(othr, obj);
		},
		/**
		 * 查看详情
		 * 
		 * @param uuid project.uuid
		 */
		lookDetail:function(uuid,_this) {
			$(_this).parent().parent("tr").css("color", "red");
			parent.createPage("项目详情", "../html/project/project_detail.html?uuid=" + uuid, true, "project_detail");
		},
		/**
		 * 编辑项目
		 */
		edit:function(id){
			parent.createPage("编辑项目", "../html/project/edit_project.html?id=" + id, true, "edit_project");
		},
		/**
		 * 导出表格
		 */
		exportExcel:function(){
			if(Table_Obj==null){
				layer.alert("请先检索数据！");
				return;
			}
			var rows = Table_Obj.getCheckedItem();
			var column = new Array();
			column.push(Table_Obj.getColumns());
			var dataList={};
			dataList.column=column;
			dataList.rows=rows;
			dataList.type="project";
			if(rows.length == 0){
				layer.alert("未选择导出的数据！");
			}else{
		        var tempForm = document.createElement("form");
		        document.body.appendChild(tempForm);
		        tempForm.method="post";
		        tempForm.action="/crm/exportExcel";
		        var hideInput = document.createElement("input");
		        hideInput.type="hidden";
		        hideInput.name= "datas";
		        hideInput.value= JSON.stringify(dataList);
		        tempForm.appendChild(hideInput);
		        tempForm.submit();
		    }
		},
		/**
		 * 导出所有查询出来的数据
		 */
		exportAllToExcel:function(){
			if(Table_Obj==null){
				layer.alert("请先检索数据！");
				return;
			}
			var pageSize = $("#total_records").text();
			var pageIndex = 1;
			var obj = heigthSearch.getParam();
			obj.pageSize = pageSize;
			obj.pageIndex = pageIndex;
			var column = new Array();
			column.push(Table_Obj.getColumns());
			obj.column=JSON.stringify(column);
			var tempForm = document.createElement("form");
	        document.body.appendChild(tempForm);
	        tempForm.method="post";
	        tempForm.action="/crm/project/exportExcel";
	        var hideInput = document.createElement("input");
	        hideInput.type="hidden";
	        hideInput.name= "datas";
	        hideInput.value= JSON.stringify(obj);
	        tempForm.appendChild(hideInput);
	        tempForm.submit();
		}
}